package cn.com.dcsgo.model;

import cn.com.dcsgo.constant.BusinessEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * HTTP统一返回对象
 *
 * @author Dcsgo
 * @since 2025/9/16 09:19
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Schema(title = "Result",description = "HTTP统一返回对象")
public class Result<T> {
    @Schema(description = "状态码", example = "200")
    private Integer code = 200;
    @Schema(description = "响应信息", example = "操作成功")
    private String msg = "OK";
    @Schema(description = "数据")
    private T data;

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setData(data);
        return result;
    }

    public static <T> Result<T> fail(BusinessEnum businessEnum) {
        Result<T> result = new Result<>();
        result.setCode(businessEnum.getCode());
        result.setMsg(businessEnum.getDesc());
        return result;
    }

    public static <T> Result<T> fail(Integer code, String msg) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;

    }

    /**
     * 根据传入参数flag响应HTTP请求
     *
     * @param flag 为true调用success进行响应，为false调用fail进行响应
     * @return success(null)或fail(BusinessEnum.OPERATION_FAIL)
     */
    public static Result<Void> handle(boolean flag) {
        if (flag) {
            return success(null);
        }
        return fail(BusinessEnum.OPERATION_FAIL);
    }
}
